Code
library(tidyverse)
library(here)
library(sf)
library(tmap)
library(spatstat)
library(patchwork)The dataset for this task is California oil spill tracking data from in 2008, stored in a CSV file, and California county borders, stored as a ShapeFile.
The purpose of this task is to analyze the oil spill tracking data and plot the spill incidents on the CA county ShapeFileas an interactive map. Then, the second part of the task is to create a cloropleth or ‘heatmap’ showing which counties were the worst affected by oil spill incidents in 2008.
I accomplished the first part of this task- the interactive oil spill incident map- in three steps. First, I converted the oil spill incident CSV into a ShapeFileso I could display the information spatially. Then, I matched up the new oil spill ShapeFile’s CRS code to the one in the CA county file so they would align on the map. Finally, I used the T_Map package to create an interactive map.
I accomplished the second part of the task by creating a merged ShapeFile with both the oil spill incident and county boundary ShapeFiles. Then, I made a new ShapeFile, storing only the total count of oil spill incidents by county. I then used a custom gradient color scheme to differentiate the low oil spill incident count counties from the ones with a high oil spill incident count.
I will be doing the optional third task.
library(tidyverse)
library(here)
library(sf)
library(tmap)
library(spatstat)
library(patchwork)ca_countyborder_sf <- read_sf(here('data', 'CA_Counties'), layer = 'CA_Counties_TIGER2016') %>%
janitor::clean_names()
oilspill_df <- read_csv(
here('data', 'Oil_Spill_Incident_Tracking_[ds394].csv')) %>%
janitor::clean_names()# Converting the Oil Spill dataframe into a shapefile so it can interact with the county border shapefile
oilspillshape_sf <- oilspill_df %>%
drop_na(x, y) %>%
st_as_sf(coords = c("x", "y"))# Checking the CRS of county border file
#st_crs(ca_counties_sf) #ID ["EPSG", 3857]
# Set Oil Spill shapefile CRS to CRS of the county border file.
st_crs(oilspillshape_sf) <- 3857tmap_mode(mode = 'view')
#Created the map with the Go Gauchos color scheme.
tm_shape(ca_countyborder_sf) +
tm_fill(col = "cornflowerblue") +
tm_shape(oilspillshape_sf) +
tm_dots(col = "gold2")+
tm_layout(title = "Oil Spills in California, 2008", inner.margins = c(0,0,.1,0), title.size = 8)